home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
formatvb
/
sample2.bas
< prev
Wrap
BASIC Source File
|
1999-08-20
|
25KB
|
654 lines
Attribute VB_Name = "OrderCommon"
Option Explicit
Public Const scPREV_INSTANCE_RUNNING = "Cannot run more than one instance of this application. Please use the currently running one."
Private Const icINVALID_PARM_COUNT = 123
Private Const scZIP_CODE = "ZipCode"
' Modes for frmActor
Public Enum ACTOR_MODE
icADD_ADDRESS = 1
icEDIT_ADDRESS = 2
icDISPLAY_ADDRESS = 3
icADD_CUSTOMER = 4
End Enum
' Modes for frmActors
Public Enum ACTORS_MODE
icGET_CUSTOMER = 1
icGET_SUPPLIER = 2
icGET_EMPLOYEE = 3
End Enum
' Modes for frmOrder
Public Enum ORDER_MODE
icBASE_MENU = 1
icADD_PURCHASE_ORDER = 2
icDISPLAY_PURCHASE_ORDER = 3
icDISPLAY_SALES_ORDER = 6
End Enum
' Modes for frmOrders
Public Enum ORDERS_MODE
icPURCHASE_ORDERS = 1
icSALES_ORDERS = 2
End Enum
Dim iPrms As Integer
Dim sQry As String
' Column types for VToSQL
Private Enum COL_TYPE
icDATE = 1
icFOREIGN_KEY = 2
icNUMBER = 3
icOTHER = 4
icSTRING = 5
icNON_EMPTY_STRING = 6
End Enum
Dim sQry As String
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
ByRef lpType As Long, ByVal szData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" _
(ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, _
ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, _
lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, _
lpdwDisposition As Long) As Long
Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" _
(ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, _
lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, _
lpcbClass As Long, lpftLastWriteTime As FILETIME) As Long
Public Function Insert(Optional oContact As cContact, _
Optional oCustomer As cCustomer, _
Optional oEmployee As cEmployee) As Boolean
' Insert contact, customer or employee data object into database
Dim iPrms As Integer
Dim sQry As String
' Initialize return value
Insert = False
' Allow only one parameter
iPrms = 0
If Not oContact Is Nothing Then
iPrms = iPrms + 1
End If
If Not oCustomer Is Nothing Then
iPrms = iPrms + 1
End If
If Not oEmployee Is Nothing Then
iPrms = iPrms + 1
End If
If iPrms <> 1 Then
Err.Raise icINVALID_PARM_COUNT, OBJNAME, scINVALID_PARM_COUNT
Exit Function
End If
If Not oContact Is Nothing Then
' Inserting a Contact
' Construct SQL statement
With oContact
sQry = "INSERT INTO contacts (ContactType, _
LastName, _
FirstName, _
Address1, _
Address2, _
City, _
State, _
ZipCode, _
PhoneNumber) VALUES (" & _
VToSQL(.ContactType, icNON_EMPTY_STRING) & _
scCS & VToSQL(.LastName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.FirstName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.Address1, icNON_EMPTY_STRING) & _
scCS & VToSQL(.Address2, icNON_EMPTY_STRING) & _
scCS & VToSQL(.City, icNON_EMPTY_STRING) & _
scCS & VToSQL(.State, icNON_EMPTY_STRING) & _
scCS & VToSQL(.ZipCode, icNON_EMPTY_STRING) & _
scCS & VToSQL(.ContactPerson, icNON_EMPTY_STRING) & _
scCS & VToSQL(.PhoneNumber, icNON_EMPTY_STRING) & _
")"
End With
' Execute query and return success
If ExecQuery(scDSN, sQry) Then Insert = True
Exit Function
End If
If Not oCustomer Is Nothing Then
' Inserting a Customer
' Construct SQL statement
With oCustomer
sQry = "INSERT INTO customers (NickName, LastName, FirstName, Address1, Address2, City, State, ZipCode, PhoneNumber) VALUES (" & _
VToSQL(.NickName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.LastName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.FirstName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.Address1, icNON_EMPTY_STRING) & _
scCS & VToSQL(.Address2, icNON_EMPTY_STRING) & _
scCS & VToSQL(.City, icNON_EMPTY_STRING) & _
scCS & VToSQL(.State, icNON_EMPTY_STRING) & _
scCS & VToSQL(.ZipCode, icNON_EMPTY_STRING) & _
scCS & VToSQL(.PhoneNumber, icNON_EMPTY_STRING) & _
")"
End With
' Execute query and return success
If ExecQuery(scDSN, sQry) Then Insert = True
Exit Function
End If
If Not oEmployee Is Nothing Then
' Inserting an Employee
' Construct SQL statement
With oEmployee
sQry = "INSERT INTO employees (Alias, LastName, Firstname, MI, SSN, Address1, Address2, City, State, ZipCode, HomePhone, CellPhone, EmergencyContact, EmergencyPhone, HireDate, Password) VALUES (" & _
VToSQL(.Alias, icNON_EMPTY_STRING) & _
scCS & VToSQL(.LastName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.FirstName, icNON_EMPTY_STRING) & _
scCS & VToSQL(.MI, icNON_EMPTY_STRING) & _
scCS & VToSQL(.SSN, icNON_EMPTY_STRING) & _
scCS & VToSQL(.Address1, icNON_EMPTY_STRING) & _
scCS & VToSQL(.Address2, icNON_EMPTY_STRING) & _
scCS & VToSQL(.City, icNON_EMPTY_STRING) & _
scCS & VToSQL(.State, icNON_EMPTY_STRING) & _
scCS & VToSQL(.ZipCode, icNON_EMPTY_STRING) & _
scCS & VToSQL(.HomePhone, icNON_EMPTY_STRING) & _
scCS & VToSQL(.CellPhone, icNON_EMPTY_STRING) & _
scCS & VToSQL(.EmergencyContact, icNON_EMPTY_STRING) & _
scCS & VToSQL(.EmergencyPhone, icNON_EMPTY_STRING) & _
scCS & VToSQL(.HireDate, icDATE) & _
scCS & VToSQL(.Password, icNON_EMPTY_STRING) & _
")"
End With
' Execute query and return success
If ExecQuery(scDSN, sQry) Then Insert = True
Exit Function
End If
End Function
Public Function InsertOrder(oOrder As cOrder, cOrderDetails As cOrderDetails) As Boolean
' Insert an order and order details into database
Dim oConn As ADODB.Connection
Dim oRset As ADODB.Recordset
Dim oOrderDet As cOrderDetail
Dim lOrderId As Long
Dim lErrNo As Long
Dim sErrDesc As String
'default to false for function
InsertOrder = False
'enable error handler
On Error GoTo ErrorHandler
'Get connection
Set oConn = New ADODB.Connection
oConn.Open scDSN
oConn.BeginTrans
Set oRset = New ADODB.Recordset
Set oRset.ActiveConnection = oConn
oRset.CursorType = adOpenKeyset
oRset.LockType = adLockOptimistic
oRset.Open "Orders", , , , adCmdTable
' record to orders table
oRset.AddNew
With oOrder
If .CustomerId Then oRset!CustomerId = .CustomerId
If .ShippingHandling Then oRset!ShippingHandling = .ShippingHandling
If .Tax Then oRset!Tax = .Tax
End With
oRset.Update
' get PKId from order record for order details
lOrderId = oRset!PKId
oRset.Close
Set oRset = Nothing
Set oRset = New ADODB.Recordset
Set oRset.ActiveConnection = oConn
oRset.CursorType = adOpenKeyset
oRset.LockType = adLockBatchOptimistic
oRset.Open "OrderDetails", , , , adCmdTable
For Each oOrderDet In cOrderDetails
oRset.AddNew
With oOrderDet
oRset!OrderId = lOrderId
oRset!ItemId = .ItemId
oRset!UnitPrice = .UnitPrice
oRset!Quantity = .Quantity
End With
oRset.Update
Next
oRset.UpdateBatch
oRset.Close
Set oRset = Nothing
oConn.CommitTrans
oConn.Close
Set oConn = Nothing
'looks like everything worked so set success and exit
InsertOrder = True
Exit Function
'if we're here there then's been an error so process
ErrorHandler:
'store incoming values
lErrNo = Err.Number
sErrDesc = Err.Description
'roll back the transaction, close connection, and signal failure
On Error Resume Next
oConn.RollbackTrans
oConn.Close
InsertOrder = False
On Error GoTo 0
Err.Raise lErrNo, OBJNAME, sErrDesc
End Function
Public Function Update(Optional oContact As cContact, _
Optional oCustomer As cCustomer, _
Optional oEmployee As cEmployee, _
Optional oOrder As cOrder) As Boolean
' Update contact, customer, employee or order in database
Dim iPrms As Integer
Dim sQry As String
' Initialize return value
Update = False
' Allow only one parameter
iPrms = 0
If Not oContact Is Nothing Then
iPrms = iPrms + 1
End If
If iPrms <> 1 Then
Err.Raise icINVALID_PARM_COUNT, OBJNAME, scINVALID_PARM_COUNT
Exit Function
End If
If Not oContact Is Nothing Then
' Updating a Contact
With oContact
sQry = "UPDATE Contacts SET " & _
scCONTACT_TYPE & " = " & VToSQL(.ContactType, icNON_EMPTY_STRING) & _
scCS & scLAST_NAME & " = " & VToSQL(.LastName, icNON_EMPTY_STRING) & _
scCS & scFIRST_NAME & " = " & VToSQL(.FirstName, icNON_EMPTY_STRING) & _
scCS & scADDRESS_1 & " = "